/*

Copyright (C) 2007 Manfred Gschweidl (manfred.gschweidl@students.fhv.at)

This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.

This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA

*/

#include <octave/oct.h>

#include <sstream>
#include <string>


#include "globuscpp/CredManager.h"
#include "globuscpp/GramJobCallback.h"

using namespace std;
using namespace globus_common;
using namespace globus_job;

static CredManager credMgr;

DEFUN_DLD(gt_jobsubmit, 
          args, 
          , 
          "Submit a job to Globus GRAM."
          ) 
{	
	int argNum;
	string rsl;
	string host;
	string jobHandle;
	bool success = false;
	
	// arg(0) => url
	argNum = args.length();
	
	if (argNum != 2)  {
		error("Invalid arguments");	
		error("gt_jobsubmit('RSL', 'host')");
		return octave_value(false);
	}

	// parse args
	rsl = 	args(0).string_value();
	host = args(1).string_value();
	
	if(rsl.empty()) {
		cerr << "No RSL provided." << endl;
		return octave_value(false);
	}
	
	if(host.empty()) {
		cerr << "No execution host provided." << endl;
		return octave_value(false);
	}
	
	// check for existing certificate or aquire one                                          
	if ( !credMgr.hasCredential() ) {
		cerr << "You are not authorized within the grid." << endl;
		return octave_value(false);
	}
	
    GramJobCallback callbackServer;

    GramJob job = GramJob(&callbackServer);
    //cout << job->getCredential() << endl;
    job.setOnlyGetJobHandle(true);
    
    job.submit(host,rsl);
    job.wait();

	jobHandle = job.getJobContact();
	//delete job;
    return octave_value(jobHandle);
}

DEFUN_DLD(gt_jobstatus, 
          args, 
          , 
          "Get status for an existing GRAM job."
          ) 
{	
	int argNum;
	string jobhandle;
	bool success = false;
	
	// arg(0) => url
	argNum = args.length();
	
	if (argNum != 1)  {
		error("Invalid arguments");	
		error("gt_jobstatus('jobhandle')");
		return octave_value(false);
	}

	// parse args
	jobhandle = args(0).string_value();
	
	if(jobhandle.empty()) {
		cerr << "No handle provided." << endl;
		return octave_value(false);
	}
	
	// check for existing certificate or aquire one                                          
	if ( !credMgr.hasCredential() ) {
		cerr << "You are not authorized within the grid." << endl;
		return octave_value(false);
	}
	
    GramJob job(jobhandle);
    
    int status = job.getJobStatus();
    cout << "Status: " << GramJob::printJobStatus(status) << endl;
    return octave_value(status);
}

DEFUN_DLD(gt_jobkill, 
          args, 
          , 
          "Cancel a running GRAM job."
          ) 
{	
	int argNum;
	string jobhandle;
	bool success = false;
	
	// arg(0) => url
	argNum = args.length();
	
	if (argNum != 1)  {
		error("Invalid arguments");	
		error("gt_jobkill('jobhandle')");
		return octave_value(false);
	}

	// parse args
	jobhandle = args(0).string_value();
	
	if(jobhandle.empty()) {
		cerr << "No handle provided." << endl;
		return octave_value(false);
	}
	
	// check for existing certificate or aquire one	                                            
	if ( !credMgr.hasCredential() ) {
		cerr << "You are not authorized within the grid." << endl;
		return octave_value(false);
	}
	
    GramJob job(jobhandle);
    success = job.cancel();        
    return octave_value(success);
}
